<?php

namespace App\HttpController\Applet\Xingkong;

use App\Model\WechatsModel;
use App\Utility\Cache;
use App\Utility\CacheDatas;
use Carbon\Carbon;

class IndexController extends AuthController {

    //获取视频列表
    public function index(){
        $request= $this->request();
        $chanel = $request->getRequestParam('chanel_id') ?: 0;
        $option = $request->getRequestParam('option') ?: 0;
        $name = 'datas-'.$this->applet.'-'.$chanel;
        $cache = new CacheDatas();
        if(empty($cache->has($name))){
            $appletVideoName = $this->appletVideoModel($this->applet);
            $model = new $appletVideoName;
            $list = $model->select('videos_xingkong.id,link,title,videos_xingkong.thumb,c.name,c.thumb as avatar')
                ->join('categorys as c','c.id = videos_xingkong.cateid')
                ->where('status',1)
                ->where(function($query) use($chanel,$option){
                    if ($chanel) $query->where('chanel_id',$chanel);
                    if ($option) $query->where('`option`',2);
                })->get();
            $cache->sAddBatch($name,$list,$this->cacheTime);
            unset($model);
        }
        $lists = $cache->sRandBatch($name,40);
        unset($cache);
        return $this->writeJson(0,$lists);
    }


    //获取公众号链接文章
    public function wechatLink(){
        $request = $this->request();
        $position = $request->getRequestParam('position') ?? 1;
        $cache = new CacheDatas();
        $name = 'wechat-'.$this->applet.'-'.$position;
        if(empty($cache->has($name))){
            $appletModel = new WechatsModel();
            $list = $appletModel->where('position',$position)
                ->where('name',$this->applet)
                ->take(10)
                ->get();
            $cache->set($name,$list,$this->cacheTime);
            unset($model);
        }
        $lists = $cache->get($name);
        unset($cache);
        return $this->writeJson(0,$lists);
    }

    //获取bannner
    public function getBanner(){
        $name = 'banners-'.$this->applet;
        $cache = new CacheDatas();
        if(empty($cache->has($name))){
            $appletVideoName = $this->appletVideoModel($this->applet);
            $model = new $appletVideoName;
            $list = $model->select('id,title,thumb,link')
                ->where('banner', 2)
                ->take(5)
                ->get();
            $cache->set($name,$list,$this->cacheTime);
            unset($model);
        }
        $lists = $cache->get($name);
        unset($cache);
        return $this->writeJson(0, $lists);
    }

    //详情随机列表
    public function randList(){
        $name = 'datas-'.$this->applet.'-all';
        $cache = new CacheDatas();
        if(empty($cache->has($name))){
            $appletVideoName = $this->appletVideoModel($this->applet);
            $model = new $appletVideoName;
            $list = $model->select('id,title,thumb,link')
                ->where('status',1)
                ->get();
            $cache->sAddBatch($name,$list,$this->cacheTime);
            unset($model);
        }
        $lists = $cache->sRandBatch($name,20);
        unset($cache);
        return $this->writeJson(0, $lists);
    }

    //获取固定配置
    public function config(){
        $request = $this->request();
        $title = trim($request->getRequestParam('name')) ?: '';
        if(!$title){
            return $this->writeJson(1,null,'参数不正确');
        }
        $name = 'links-'.$this->applet.'-configs-'.$title;
        $cache = new CacheDatas();
        if(empty($cache->has($name))){
            $appletVideoName = $this->appletConfigModel($this->applet);
            $model = new $appletVideoName;
            $info = $model->select('value')->where('name',$title)->first();
            $cache->set($name,$info['value'],$this->cacheTime);
            unset($model);
        }
        $link = $cache->get($name);
        unset($cache);
        return $this->writeJson(0,$link);
    }

    //获取详情
    public function info(){
        $request= $this->request();
        $id = $request->getRequestParam('id');
        $share = $request->getRequestParam('share') ?? null;
        $openid = $request->getRequestParam('openid') ?? '';
        $name = 'info-'.$this->applet.'-'.$id;
        $cache = new CacheDatas();
        if(empty($cache->has($name))){
            $appletVideoName = $this->appletVideoModel($this->applet);
            $model = new $appletVideoName;
            $info = $model->select('id,title,thumb,link,userid')->find($id);
            if(!$info){
                //数据不存在随机返回一条信息
                $info = $model->select('id,title,thumb,link,userid')->inRandomOrder()->first();
            }
            $cache->set($name,$info,$this->cacheTime);
            unset($model);
        }
        $data = $cache->get($name);
        unset($cache);
        //开启任务-创建统计自增推送队列
        $queueData = ['id' => $id, 'admin_id' => $data['userid'], 'share' => $share, 'openid' => $openid, 'applet' => $this->applet, 'applet_type' => 'video',];
        $queue = new Cache();
        $queue->onQueue('count-inc');
        $queue->push($queueData);
        unset($queue);
        return $this->writeJson(0,$data);
    }


    //收集模板消息的formid
    public function formId(){
        $request = $this->request();
        $openid = trim($request->getRequestParam('openid')) ?: '';
        $formid = trim($request->getRequestParam('formid')) ?: '';
        if($openid){
            $queue = new Cache();
            $queue->onQueue('formid');
            $queue->push([
                'openid' => $openid,
                'expire' => Carbon::now()->addDays(7)->timestamp,
                'formid' => $formid,
                'name'   => $this->applet
            ]);
            unset($queue);
            return $this->writeJson(0);
        }
        return $this->writeJson(1,null,'参数错误');
    }


    //更新预约状态
    public function reserve(){
        $request= $this->request();
        $openid = trim($request->getRequestParam('openid')) ?: '';
        $status = trim($request->getRequestParam('status')) ?: 1;
        if(empty($openid)){
            return $this->writeJson(1,null,'参数不正确');
        }
        $userModelName = $this->appletUserModel($this->applet);
        $userModel = new $userModelName;
        $userInfo = $userModel->select('id,status')->where('openid',$openid)->first();
        if($userInfo){
            if($userInfo['status'] == $status) return $this->writeJson(0);
            $userInfo['status'] = $status;
            $userModel->update($userInfo);
            return $this->writeJson(0);
        }
        return $this->writeJson(1,null,'数据不存在');
    }





}
